package com.zhengtoudai.util;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.zhengtoudai.R;


/**
 * xiejingwen
 */
public class TitleUtil {
    private AppCompatImageView mTvLeft;
    private AppCompatTextView mTvTitle;
    private AppCompatTextView mTvRight;
    private Toolbar mToolbar;
    private AppCompatActivity mActivity;
    //activity构造
    public TitleUtil(AppCompatActivity activity) {
       mActivity=activity;
        mToolbar= (Toolbar) activity.findViewById(R.id.toolbar);
        if (mToolbar==null){
            return;
        }
        mTvLeft= (AppCompatImageView) activity.findViewById(R.id.tv_left);
        mTvTitle= (AppCompatTextView) activity.findViewById(R.id.tv_title);
        mTvRight= (AppCompatTextView) activity.findViewById(R.id.tv_right);
        mActivity.setSupportActionBar(mToolbar);
        mActivity.getSupportActionBar().setDisplayShowTitleEnabled(false);
    }
    //fragment构造
    public TitleUtil(AppCompatActivity activity,View view) {
        mActivity=activity;
        mToolbar= (Toolbar) view.findViewById(R.id.toolbar);
        if (mToolbar==null){
            return;
        }
        mTvLeft= (AppCompatImageView) view.findViewById(R.id.tv_left);
        mTvTitle= (AppCompatTextView) view.findViewById(R.id.tv_title);
        mTvRight= (AppCompatTextView) view.findViewById(R.id.tv_right);
        mActivity.setSupportActionBar(mToolbar);
        //设置默认的title为隐藏
        mActivity.getSupportActionBar().setDisplayShowTitleEnabled(false);
    }
    public Toolbar getToolbar(){
        return mToolbar;
    }
    private void hintView(){
        if (mTvLeft.isShown()){
            mTvLeft.setVisibility(View.GONE);
        }
        if (mTvTitle.isShown()){
            mTvTitle.setVisibility(View.GONE);
        }
        if (mTvRight.isShown()){
            mTvRight.setVisibility(View.GONE);
        }
    }
    /**
     * 设置标题，与显示返回箭头
     * @param title
     */
    public void setTitle(String title){
        hintView();
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText(title);
        isShowNavigation(true);
    }

    public void setTitle(int resId,boolean isShow){
        hintView();
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText("");
        mTvTitle.setBackgroundResource(resId);
        isShowNavigation(isShow);
    }


    /**
     *
     * @param title 标题
     * @param isShow  是否显示返回箭头
     */
    public void setTitle(String title,boolean isShow){
        hintView();
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText(title);
        isShowNavigation(isShow);
    }


    /**
     * 设置标题图片及右边文字
     * @param titleId
     * @param rightTitle
     * @param listener
     */
    public void setTitleImgOrRight(int titleId, String rightTitle, View.OnClickListener listener){
        hintView();
        mTvTitle.setVisibility(View.VISIBLE);
        mTvRight.setVisibility(View.VISIBLE);
        isShowNavigation(false);
        mTvTitle.setBackgroundResource(titleId);
        mTvRight.setText(rightTitle);
        mTvRight.setOnClickListener(listener);
    }
    /**
     * 设置标题文字及右边文字
     * @param isShowBack
     * @param title
     * @param rightTitle
     * @param listener
     */
    public void setTitleOrRight(boolean isShowBack,String title, String rightTitle, View.OnClickListener listener){
        hintView();
        mTvTitle.setVisibility(View.VISIBLE);
        mTvRight.setVisibility(View.VISIBLE);
        isShowNavigation(isShowBack);
        mTvTitle.setText(title);
        mTvRight.setText(rightTitle);
        mTvRight.setOnClickListener(listener);
    }
    /**
     * 是否显示返回箭头
     * @param isShow
     */
    private void isShowNavigation(boolean isShow){
        if (isShow){
            mTvLeft.setVisibility(View.VISIBLE);
            mTvLeft.setImageResource(R.mipmap.back);
            mTvLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mActivity.finish();
                }
            });
        }
    }


}
